package ua.exchangeratesukraine.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import ua.exchangeratesukraine.R;
import ua.exchangeratesukraine.model.CurrencyModel;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NbuAdapter extends BaseAdapter {

	private final String				LOG_TAG				= getClass().getSimpleName();
	private Context						context				= null;
	private ArrayList<CurrencyModel>	currencyModelArray	= new ArrayList<CurrencyModel>();
	private CurrencyModel				currencyModel		= null;

	// For view recycling (optimization for listview)
	static class ViewHolder {
		TextView	tvName;
		TextView	tvRate;
		TextView	tvChangeAbs;
		TextView	tvChangeRel;
	}

	public NbuAdapter(Context context, ArrayList<CurrencyModel> currencyModel) {
		this.context = context;
		this.currencyModelArray = currencyModel;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		ViewHolder holder = new ViewHolder();
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.list_item_nbu, null);
			holder.tvName = (TextView) view.findViewById(R.id.tvName);
			holder.tvRate = (TextView) view.findViewById(R.id.tvRate);
			holder.tvChangeAbs = (TextView) view.findViewById(R.id.tvChangeAbs);
			holder.tvChangeRel = (TextView) view.findViewById(R.id.tvChangeRel);
			view.setTag(holder);
		}
		if (position % 2 != 0) {
			view.setBackgroundResource(R.color.listview_item_2);
		} else {
			view.setBackgroundResource(R.color.listview_item_1);
		}
		currencyModel = ((CurrencyModel) getItem(position));
		if (	currencyModel.getCode().equals("USD")
			 || currencyModel.getCode().equals("EUR")
			 || currencyModel.getCode().equals("RUB") 
			){
			view.setBackgroundResource(R.color.listview_item_3);
		}

		if (currencyModel != null) {
			holder = (ViewHolder) view.getTag();
			holder.tvName.setText(currencyModel.getName());
			holder.tvRate.setText(currencyModel.getRate());
			holder.tvChangeAbs.setText(currencyModel.getChangeAbs());
			holder.tvChangeRel.setText(currencyModel.getChangeRel());
		}
		return view;
	}

	@Override
	public int getCount() {
		return currencyModelArray.size();
	}

	@Override
	public Object getItem(int position) {
		return currencyModelArray.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public ArrayList<CurrencyModel> getCurrencyModelArray() {
		return currencyModelArray;
	}
}
